package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.CompensationQueryObject;
import cn.wolfcode.crm.query.SaleChartQuery;
import cn.wolfcode.crm.service.IChartService;
import cn.wolfcode.crm.util.Dictionary;
import cn.wolfcode.crm.util.JsonUtil;
import com.alibaba.druid.support.json.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/chart")
public class ChartController {

    @Autowired
    private IChartService chartService;

    //理赔报表界面
    @RequestMapping("/compensationChart")
    public String compensationChart() {
        return "chart/compensation/compensationChart";
    }


    //理赔的高级查询
    @RequestMapping("/compensationQuery")
    @ResponseBody
    public Object compensationQuery(CompensationQueryObject qo) {
        return chartService.compensationQuery(qo);
    }

    //理赔柱形图
    @RequestMapping("/compensationChartByBar")
    public String compensationChartByBar(Model model, @ModelAttribute("qo") CompensationQueryObject qo) {
        List<Map<String, Object>> maps = chartService.compensationQuery(qo);
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            x.add(map.get("groupType"));
            y.add(map.get("totalAmount"));
        }
        model.addAttribute("groupType", Dictionary.COMPENSATION_CHART.get(qo.getGroupType()));
        model.addAttribute("x", JsonUtil.parseToString(x));
        model.addAttribute("y", JsonUtil.parseToString(y));
        return "chart/compensation/chartByBar";
    }

    //理赔饼状图
    @RequestMapping("/compensationChartByPie")
    public String saleByPie(Model model, @ModelAttribute("qo") CompensationQueryObject qo) {
        List<Map<String, Object>> maps = chartService.compensationQuery(qo);
        List<Object> x = new ArrayList<>();//存放标题的集合
        List<Object> datas = new ArrayList<>();//存放多个json对象
        for (Map<String, Object> map : maps) {
            x.add(map.get("groupType"));
            Map<String, Object> m = new HashMap<>();//用来存放一个json对象
            m.put("name", map.get("groupType"));
            m.put("value", map.get("totalAmount"));
            datas.add(m);
        }
        model.addAttribute("groupType",Dictionary.COMPENSATION_CHART.get(qo.getGroupType()));
        model.addAttribute("x",JsonUtil.parseToString(x));
        model.addAttribute("datas",JsonUtil.parseToString(datas));
        return "chart/compensation/chartByPie";
    }


    @RequestMapping("saleChart/view")
    public String saleChart(){
        return "chart/sale";
    }


    @RequestMapping("saleChart/query")
    @ResponseBody
    public List<?> query(SaleChartQuery qo){
        List<?> maps = chartService.querySaleChart(qo);
        return maps;
    }


    @RequestMapping("saleBybar/view")
    public String saleBybar(Model model, @ModelAttribute("qo") SaleChartQuery qo){
        //定义一个x轴变量
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();

        //获取所有的对象,然后遍历获取每一个属性和值
        List<Map<String, String>> maps = chartService.querySaleChart(qo);
        for (Map<String, String> map : maps) {
            //取出当前行中的groupType和totalAmount两个值对应存入x/y
            Object groupType = map.get("groupType");
            Object totalAmount = map.get("totalAmount");
            x.add(groupType);
            y.add(totalAmount);
        }
        //把x和y转换成json对象,并且存入到模式Attribute对象中
        model.addAttribute("groupType",Dictionary.SALE_MAP.get(qo.getGroupType()));
        model.addAttribute("x",JSONUtils.toJSONString(x));
        model.addAttribute("y",JSONUtils.toJSONString(y));
        return "chart/saleByBar";
    }

    @RequestMapping("saleByPie/view")
    public String saleByPie(Model model, @ModelAttribute("qo") SaleChartQuery qo){
        List<Object> x = new ArrayList<>();
        List<Object> datas = new ArrayList<>();
        List<Map<String, String>> maps = chartService.querySaleChart(qo);
        for (Map<String, String> map : maps) {
            //取出当前行中的groupType和totalAmount两个值对应存入x/y中
            Object groupType = map.get("groupType");
            x.add(groupType);

            Map<String,Object> obj = new HashMap<>();
            obj.put("name",groupType);
            obj.put("value",map.get("totalAmount"));
            datas.add(obj);
        }

        //转换成json对象存入到模式对象中
        model.addAttribute("groupType",Dictionary.SALE_MAP.get(qo.getGroupType()));
        model.addAttribute("x",JSONUtils.toJSONString(x));
        model.addAttribute("datas",JSONUtils.toJSONString(datas));
        return "chart/saleByPie";
    }
}
